package managedbean.PurchaseOrder;

import entity.LineItem;
import entity.Product;
import entity.SaleLead;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.SaleInquiryBeanLocal;
import session.stateless.PurchaseOrderBeanLocal;
import session.stateless.SaleQuotationBeanLocal;

@ManagedBean(name="addDirectPurchaseOrder")
@ViewScoped
public class AddDirectPurchaseOrderManagerBean implements Serializable {
    public AddDirectPurchaseOrderManagerBean () {}
    
    @EJB
    private PurchaseOrderBeanLocal purchaseOrderBeanLocal;
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    
    private SaleLead selectedSaleLead;
    private Long purchaseOrderId;
    private List<LineItem> lineItems;

    private String statusMessage;

    public void initView(PhaseEvent event)
    {
        if (selectedSaleLead == null)
            selectedSaleLead = (SaleLead) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleLead");            
        try
        {
            if(lineItems == null)
            {
                List<Product> products = saleInquiryBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);

                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public Long getPurchaseOrderId() {
        return purchaseOrderId;
    }
    public void setPurchaseOrderId(Long purchaseOrderId) {
        this.purchaseOrderId = purchaseOrderId;
    }

    public String getStatusMessage() {
        return statusMessage;
    }
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public PurchaseOrderBeanLocal getPurchaseOrderBeanLocal() {
        return purchaseOrderBeanLocal;
    }

    public void setPurchaseOrderBeanLocal(PurchaseOrderBeanLocal purchaseOrderBeanLocal) {
        this.purchaseOrderBeanLocal = purchaseOrderBeanLocal;
    }

    public SaleLead getSelectedSaleLead() {
        return selectedSaleLead;
    }

    public void setSelectedSaleLead(SaleLead selectedSaleLead) {
        this.selectedSaleLead = selectedSaleLead;
    }
    
    public void saveNewPurchaseOrder(ActionEvent event)
    {
          try {
            List<LineItem> lineItemList = new ArrayList<LineItem>();
            LineItem temp = new LineItem();
            for (int i = 0; i < lineItems.size(); i++) {
                if (lineItems.get(i).getLineItemQuantity() != null) {
                    temp = saleQuotationBeanLocal.createLineItem_Discount(lineItems.get(i).getProduct(), lineItems.get(i).getLineItemQuantity());
                    lineItemList.add(temp);
                }
            }
            purchaseOrderId = purchaseOrderBeanLocal.createDirectPurchaseOrder_SaleLead(selectedSaleLead.getRegistrationNumber(), lineItemList);
            statusMessage = "New Sale Order Saved Successfully.";

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Sale Order Result: " + statusMessage + " (New Sale Order ID: " + purchaseOrderId + ")", ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }
}
